﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Visio = Microsoft.Office.Interop.Visio;
using System.Collections.Specialized;
using System.Configuration;
using SPNetworkTopology.ExternalWindows.ViewModel;

namespace SPNetworkTopology
{
    class GetRecordSetRowIDS : GenerateDiagram
    {
        /// <summary>
        /// Flag which says config file is corrupted or not
        /// </summary>
        public bool IsConfigFileCorrupted = false;

        private KeyValueConfigurationCollection AppSettings;

        private string Server = "Server";

        private string SharePointService = "SharePoint Service";

        private string Farm = "Farm";        

        public GetRecordSetRowIDS()            
        {
            Utility utility = new Utility();
            Configuration config = ConfigurationManager.OpenExeConfiguration(utility.ConfigFilePath);

            AppSettings = config.AppSettings.Settings;            

            if (!AppSettings.AllKeys.Contains("Object Type") ||
               !AppSettings.AllKeys.Contains("Server Name") ||
               !AppSettings.AllKeys.Contains("Service Name"))
            {
                this.IsConfigFileCorrupted = true;
                return;
            }

            base.ObjectTypeFriendlyName = AppSettings["Object Type"].Value;
            base.ServerNameObjectFriendlyName = AppSettings["Server Name"].Value;
            base.ServiceNameObjectFriendlyName = AppSettings["Service Name"].Value;

            //Get the current document recordset
            this.GetSPFarmInfoRecordset();
        }

        private void GetSPFarmInfoRecordset()
        {
            Visio.DataRecordsets recordSetCollection = base.VisioDocument.DataRecordsets;

            foreach (Visio.DataRecordset recordset in recordSetCollection)
            {
                if (recordset.DataColumns.Cast<Visio.DataColumn>().
                Select(dataColumn => dataColumn.DisplayName).Contains(this.ObjectTypeFriendlyName))
                {
                    base.CurrentDocumentRecordSet = recordset;
                    break;
                }
            }

            if (base.CurrentDocumentRecordSet != null)
            {
                this.GetDataRowIDS();
            }
        }

        private void GetDataRowIDS()
        {
            List<string> currentDocumentRecordSetDataColumns = base.CurrentDocumentRecordSet.DataColumns.Cast<Visio.DataColumn>().
                                                               Select(dataColumn => dataColumn.DisplayName).ToList();

            //Get the index of object type column
            int objectTypeColumnIndex = currentDocumentRecordSetDataColumns.IndexOf(base.ObjectTypeFriendlyName);
            int serverNameColumnIndex = currentDocumentRecordSetDataColumns.IndexOf(base.ServerNameObjectFriendlyName);
            int serviceNameColumnIndex = currentDocumentRecordSetDataColumns.IndexOf(base.ServiceNameObjectFriendlyName);

            Array allRowIDs = CurrentDocumentRecordSet.GetDataRowIDs("");

            for (int index = 0; index < allRowIDs.Length; index++)
            {
                Array rowData = base.CurrentDocumentRecordSet.GetRowData((int)allRowIDs.GetValue(index));
                string value = rowData.GetValue(objectTypeColumnIndex).ToString();
                
                if (value.Equals(this.Farm))
                {
                    base.FarmRowID = (int)allRowIDs.GetValue(index);
                }
                else if (value.Equals(this.Server))
                {
                    base.ServerRowIDList.Add((int)allRowIDs.GetValue(index));
                }
                else if (value.Equals(this.SharePointService))
                {
                    ServerRowIDCollection collection = new ServerRowIDCollection();

                    string serviceName = rowData.GetValue(serviceNameColumnIndex).ToString();

                    //Consider only the list of selected SharePoint service
                    if (ServiceLinking.SelectedService.Contains(serviceName))
                    {
                        collection.RowID = (int)allRowIDs.GetValue(index);
                        collection.ServerName = rowData.GetValue(serverNameColumnIndex).ToString();
                        collection.ServiceName = serviceName;

                        base.ServiceRowIDList.Add(collection);
                    }
                }
            }
        }
    }
}
